<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head lang="en">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"/>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="format-detection" content="email=no"/>
    <meta charset="UTF-8">
    <script type="text/javascript" th:src="@{${session.baseUrl}+'js/common.js'}"></script>
    <script th:src="@{${session.baseUrl}+'js/jquery.js'}"></script>
    <link th:href="@{${session.baseUrl}+'style/reset.css'}" rel="stylesheet" type="text/css">
    <link th:href="@{${session.baseUrl}+'style/base.css'}" rel="stylesheet" type="text/css">
    <link th:href="@{${session.baseUrl}+'style/header.css'}" rel="stylesheet" type="text/css">
    <link th:href="@{${session.baseUrl}+'style/dialog.css'}" rel="stylesheet" type="text/css">
    <link th:href="@{${session.baseUrl}+'style/global.css'}" rel="stylesheet" type="text/css">
    <script th:src="@{${session.baseUrl}+'js/dialog.js'}"></script>
    <style>
        body{
            background: #f2f2f2;
        }
        header b{
            float: right;
            font-size: 0.26rem;
            color: #fff;
            font-weight: normal;
            margin-right: 0.3rem;
        }
        textarea{
            width: 100%;
            font-size: 0.26rem;
            color: #999;
            background: #fff;
            padding:0.2rem 0.3rem 0 0.3rem;
            height: 3.5rem;
        }
        form{
            position: relative;
        }
        form span{
            position: absolute;
            bottom:0.2rem;
            right: 0.3rem;
            font-size: 0.26rem;
            color: #999;
        }
    </style>
    <title>修改昵称</title>
</head>
<body>
<header><span onclick="javascript:history.go(-1)"></span>修改昵称<b onclick="updatenick()">完成</b></header>
<form action="">
    <textarea name="" id="write" cols="12" rows="10" placeholder="请输入您的昵称" maxlength="12"></textarea>
    <span id="count">0/12</span>
</form>
<script type="text/javascript" th:inline="javascript">
    var basePath = /*[[${#httpServletRequest.getScheme() + "://" + #httpServletRequest.getServerName() + ":" + #httpServletRequest.getServerPort() + #httpServletRequest.getContextPath()}]]*/;

    function updatenick(){
        var x = document.getElementById("write").value.length;
        var name = $("#write").val();
        var myReg = /^[A-Za-z][A-Za-z0-9]{5,11}$/;
        if (name == '') {
            flag=2;
        } else if (x <= 5) {
            flag=3;
        }else if(!myReg.test(name)){
            flag=3;
        } else {
            var name1 = $("#write").val();
            $.ajax({
                url: basePath + '/memberUser/isName',
                type: "get",
                dataType: "JSON",
                data: {
                    name: name1,
                },
                contentType: 'application/json;charset=UTF-8',
                //async: false,
                success: function (data) {
                    if (data.msg == "no") { //
                        $('body').dailog({type: 'warning', title: '错误', discription: '此昵称已经被注册'});
                        self.setInterval(function () {
                            location.reload();
                        }, 2000)
                    } else if (data.msg == "ok") {
                        flag=1;
                        if(flag==2){
                            $('body').dailog({type: 'warning', title: '错误', discription: '昵称不能为空'});
                            self.setInterval(function () {
                                location.reload();
                            }, 2000)
                        }else if(flag==3){
                            $('body').dailog({type: 'warning', title: '错误', discription: '首位必须为字母，6-12位'});
                            self.setInterval(function () {
                                location.reload();
                            }, 2000)
                        }else{
                            var nick = $("#write").val();
                            $.ajax({
                                url: basePath + '/wap/memberUser/updateNick',
                                type: "get",
                                dataType: "JSON",
                                data: {
                                    nick: nick,
                                },
                                contentType: 'application/json;charset=UTF-8',
                                //async: false,
                                success: function (data) {
                                    if (data.msg == "ok") { //
                                        $('body').dailog({type: 'success', title: '修改昵称', discription: '更改成功'});
                                        self.setInterval(function () {
                                            window.location.href = basePath + "/wap/personal.html";
                                        }, 2000)
                                    } else if (data.msg == "no") {
                                        $('body').dailog({type: 'warning', title: '修改昵称', discription: '更改失败'});
                                        self.setInterval(function () {
                                            location.reload();
                                        }, 2000)
                                    }

                                }
                            })
                        }
                    }
                },
            });
        }

    }


    $(function(){
        $("#write").on("input",function(){
            var len=$(this).val().length;
            $("#count").html(len+"/12");
        });
    });



</script>
</body>
</html>